package com.adcok.lcdns.activity;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.adcok.lcdns.common.BaseActivity;
import com.adcok.lcdns.common.LottoAdTabActivity;
import com.adcok.lcdns.cronos.http.CNLottoAdApiSetEx;
import com.adcok.lcdns.util.ApplicationUtils;
import com.adcok.lcdns.util.CNCronosLog;
import com.adcok.lcdns.util.LottoAdConstants;
import com.adcok.lcdns.util.LottoAdOverButton;
import com.adcok.lcdns.util.RecycleUtils;
import com.adcok.lcdns.R;

/**
 * 메인 Activity
 * @author Administrator
 *
 */
public class LottoAdMainActivity extends BaseActivity {

	private Context mContext;
	
	private Intent mIntent;
	private boolean mIsJoin = false;

	/**카카오톡 친구추천 버튼*/
	private Button mKakaoBtn;
	/**로또보기 버튼*/
	private Button mLottoBtn;
	private ImageView mNewIconIv;
	private String mRcode;
	/**광고보기(수신광고) 버튼*/
	private Button mRecAdBtn;
	/**광고보기(전체) 버튼*/
	private Button mAllAdBtn;
	/**MyPage 버튼*/
	private Button mMyPageBtn;
	/**공지사항 버튼*/
	private Button mNotifyBtn;
	
	@Override
	protected int onGetContentViewResource() {
		return R.layout.lottoad_main;
	}

	@Override
	protected void onInit() {
		this.mContext = this;
		
		mIntent = getIntent();
		
		Bundle bundle  = mIntent.getExtras();
		if(bundle != null){
			if(bundle.getBoolean("join", false)){
				mIsJoin = bundle.getBoolean("join", false);
				CNCronosLog.d("mJoin ========>> "+ mIsJoin);
			} 
		}
		
        setContentView(R.layout.lottoad_main);
        
        mKakaoBtn = (Button) findViewById(R.id.button_kakao);
        mLottoBtn = (Button) findViewById(R.id.button_lotto);
        mAllAdBtn = (Button) findViewById(R.id.button_all);
        mRecAdBtn = (Button) findViewById(R.id.button_recieved);
        mMyPageBtn = (Button) findViewById(R.id.button_mypage);
        mNotifyBtn = (Button)findViewById(R.id.button_noti);
        mNewIconIv = (ImageView) findViewById(R.id.new_icon);
        
        mLottoBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					Intent intent = new Intent(mContext, LottoAdTabActivity.class);
					intent.putExtra("currentTab", 1);
					startActivity(intent);
				}
				return false;
			}
		});
        
        mAllAdBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					Intent intent = new Intent(mContext, LottoAdTabActivity.class);
					intent.putExtra("currentTab", 2);
					startActivity(intent);
				}
				return false;
			}
		});
        
        mRecAdBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					Intent intent = new Intent(mContext, LottoAdTabActivity.class);
					intent.putExtra("currentTab", 3);
					startActivity(intent);
				}
				return false;
			}
		});
        
        mMyPageBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					Intent intent = new Intent(mContext, LottoAdTabActivity.class);
					intent.putExtra("currentTab", 4);
					startActivity(intent);
				}
				return false;
			}
		});
        
        mNotifyBtn.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Drawable getBackground = v.getBackground();
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundDrawable(LottoAdOverButton.grayimg(getBackground));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					getBackground.clearColorFilter();
					v.setBackgroundDrawable(getBackground);
					Intent intent = new Intent(mContext, LottoAdTabActivity.class);
					intent.putExtra("currentTab", 5);
					startActivity(intent);
				}
				return false;
			}
		});
        
        mKakaoBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ApplicationUtils.checkPackage(mContext, LottoAdConstants.KAKAO_PACKAGE_NAME);
			}
		});
	}
	
    @Override
	public String onGetAsyncRequestUrl() {
		CNCronosLog.d("onGetAsyncRequestUrl()");
		String url = null;
		
		CNLottoAdApiSetEx MnetGlobalApiSetEx = CNLottoAdApiSetEx.getInstance();
		if(mIsJoin){
			//url = MnetGlobalApiSetEx.setJoinPushUrl(mContext, LottoAdConstants.PHONE_NUMBER);
			url = MnetGlobalApiSetEx.setJoinPushUrl(mContext, ApplicationUtils.getPhoneNumber(mContext));
			mIsJoin = false;
			return url;
		}
		//url = MnetGlobalApiSetEx.getLottoListUrl(mContext, LottoAdConstants.PHONE_NUMBER);
		url = MnetGlobalApiSetEx.getLottoListUrl(mContext, ApplicationUtils.getPhoneNumber(mContext), 1, 8);
		CNCronosLog.d("onGetAsyncRequestUrl", "url : "+url);
		
		return url;
	}
	
	@Override
	public void onAsyncCancelRequest() 
	{
		super.hideLoadingDialog();
		CNCronosLog.d("onAsyncCancelRequest()");
	}

	@Override
	public void onAsyncDataError(Element xmlObject, String errorMessage)
	{
		super.hideLoadingDialog();
		
		NodeList rcodeNode = xmlObject.getElementsByTagName("rcode");
		mRcode = rcodeNode.item(0).getFirstChild().getNodeValue();
		CNCronosLog.d("mRcode : "+ mRcode);
		if(mRcode.equals("RET0002")){
			mNewIconIv.setVisibility(View.GONE);
		} else {
			ApplicationUtils.showOkDialog(mContext, 0, getString(R.string.popup_alert), errorMessage);
		}
	}

	@Override
	public void onAsyncPreRequest()
	{
		super.showLoadingDialog();
	}

	@Override
	public void onAsyncRequestCompleted(Element xmlObject)
	{
		super.hideLoadingDialog();
			
		try {
			
			mNewIconIv.setVisibility(View.VISIBLE);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onAsyncRequestError(int errorNumber) {
		super.hideLoadingDialog();
		
		ApplicationUtils.showOkDialog(mContext,0,getString(R.string.popup_alert), ApplicationUtils.getErrorMessage(mContext, errorNumber));
		return;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		onRequestData();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		RecycleUtils.recursiveRecycle(getWindow().getDecorView().getRootView());
		System.runFinalizersOnExit(true);
        System.exit(0);
		android.os.Process.killProcess(android.os.Process.myPid());
		System.gc();
	}
}